#include "seqlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



/**
 * 链表结构应该是怎样的?
 * 1.维护一个数组
 * 2.维护一个长度的变量
 * 3.数据的数据应该是怎样的?,是否需要自行转换?
 * 链表才需要转换吗
 */
typedef struct List{
    int capacity;
    int length;
    SeqListNode **node;
} tag_List;


//创建一个空的线性表
SeqList* SeqList_Create(int capacity){
    tag_List* tl=(tag_List*)malloc(sizeof (tag_List));
    if(tl==NULL){
        perror("error occure while malloc for tag_list");
        return NULL;
    }
    memset(tl,0,sizeof(tag_List));
    tl->capacity=capacity;
    tl->length=0;
    SeqListNode ** tn=(SeqListNode**)malloc((unsigned long)capacity*sizeof(SeqListNode*));
    memset(tn,0,sizeof((unsigned long)capacity*sizeof(unsigned int*)));
    if(tn==NULL){
        perror("error occure while while malloc for tage_list_node");
        free(tl);
        return NULL;
    }
    tl->node=tn;
    return tl;
}
//销毁线性表
void SeqList_Destory(SeqList *list){
    if(list==NULL){
        perror("list can not be null");
        return ;
    }
    SeqList_Clear(list);
    tag_List* tmp=(tag_List*)list;
    if(tmp->node!=NULL){
        free(tmp->node);
    }
    return;
}
//清空线性表
void SeqList_Clear(SeqList* list){
    if(list==NULL){
        perror("list can not be null");
        return ;
    }
    tag_List* tmp=(tag_List*)list;
    tmp->length=0;
    //是否需要重置节点?
    //memset(tl,0,sizeof(tag_List));
    //memset(tmp->node,0,sizeof (tmp->capacity*sizeof(tag_List_Node)));
    return ;
}
//返回线性表list中所有元素的个数
int SeqList_Length(SeqList* list){
    if(list==NULL){
        perror("list can not be null");
        return 0;
    }
    tag_List* tmp=(tag_List*)list;
    return tmp->length;
}
//将一个node插入线性表
int SeqList_Insert(SeqList* list,SeqListNode* node,int pos){
    tag_List* tmp=NULL;
    if(list==NULL){
        perror("list can not be null");
        return -1;
    }
    tmp=(tag_List*)list;
    if(pos<-1||pos>tmp->length){
        perror("pos is invalid,which is greater than length");
        return -2;
    }
    if(tmp->length>=tmp->capacity){
        perror("list had not enough space!");
        return -3;
    }
    //搬数据?
    for(int i=tmp->length;i>pos;--i){
        //从length开始
        tmp->node[i]=tmp->node[i-1];
    }
    //设置空位值
    tmp->node[pos]=node;
    tmp->length++;
    return 0;
}
//获取一个list 位于pos 位置处的元素
SeqListNode* SeqList_Get(SeqList *list,int pos){
    if(list==NULL){
        perror("list can not be null");
        return NULL;
    }
    tag_List* tmp=(tag_List*)list;
    if(pos<0||pos>=tmp->length){
        perror("the pos is invalid");
        return NULL;
    }
    SeqListNode* node=(void*)tmp->node[pos];
    return node;
}
//删除线性表list pos位置处的元素,返回被删除的元素,NULL 表示删除失败
SeqListNode* SeqList_Delete(SeqList *list,int pos){
    //删除指定位置的元素，后面的全部往前面移动
    if(list==NULL){
        perror("list can not be null");
        return NULL;
    }
    SeqListNode *node=NULL;
    //0 1 2 3 4
    tag_List* tmp=(tag_List*)list;
    if(pos<0||pos>=tmp->length){
        perror("the pos is invalid");
        return NULL;
    }
    node=tmp->node[pos];
    for(int i=pos;i<tmp->length;i++){
        tmp->node[i]=tmp->node[i+1];
    }
    tmp->length--;
    return node;
}
